package com.chookss.home.workbench

import android.content.Intent
import android.view.View
import com.chookss.R
import com.chookss.Urls
import com.chookss.home.publish.EditVedioActivity
import com.chookss.tiku.FeedbackActivity
import com.chookss.tools.MyEvent
import com.chookss.tools.MyToast
import com.chookss.tools.XJson
import com.chookss.tools.okgoconfig.MyHttpRequest
import com.chookss.tools.okgoconfig.OkGoUtil
import com.chookss.tools.okgoconfig.callback.JsonCallback
import com.chookss.video.BaseVideoPlayActivity
import com.chookss.video.OnVideoButtonClickListener
import com.chookss.video.entity.VideoEntity
import com.chookss.video.model.VideoSourceModel
import com.lzy.okgo.model.Response
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.json.JSONException
import org.json.JSONObject
import java.io.Serializable
import java.util.*

/**
 * Date: 2021/5/20 16:17
 * Note:  视频审核
 */
class WorkVideoActivity : BaseVideoPlayActivity() {
    private lateinit var videoEntity: VideoEntity
    private lateinit var auditStatus: String
    override fun initOnCreate(): Boolean {
        var tmp = intent?.getSerializableExtra("video")
        if (tmp == null) {
            return false
        }
        videoEntity = tmp!! as VideoEntity
        auditStatus= intent?.getStringExtra("auditStatus").toString()
        return true
    }

    override fun initVideoPlayView() {
        super.initVideoPlayView()
        videoPlayView.videoAdapter.setOnUpdateHolder { holder, position, entity ->
            holder.llRight.visibility = View.GONE
            holder.tvSetsInfo.visibility = View.GONE
            holder.tvMmarquee.visibility = View.GONE
            holder.tvOnePlay.visibility = View.GONE
            if("2".equals(auditStatus)){
                holder.llPass.visibility = View.GONE
            }else{
                holder.llPass.visibility = View.VISIBLE
            }
        }
    }

    override fun createOnVideoButtonClickListener(): OnVideoButtonClickListener {
        return object : OnVideoButtonClickListener(videoModel, this) {
            override fun onVideoClick(v: View, tvComment: View, position: Int, model: VideoEntity) {
                if (v.id == R.id.tvPass) {
                    passData();
                } else if (v.id == R.id.tvNoPass) {
                    val intent = Intent(this@WorkVideoActivity, FeedbackActivity::class.java)
                    val news: MutableMap<String, String> = HashMap()
                    val lists: MutableList<Map<String, String>> = ArrayList()
                    news["id"] = videoEntity.id
                    news["type"] = "2"
                    news["projectCode"] = videoEntity.videoId
                    news["employeeCode"] = videoEntity.employeeCode
                    lists.add(news)
                    intent.putExtra("myWorkBeans", lists as Serializable)
                    intent.putExtra("type", 1)
                    startActivity(intent)
                    finish()
                } else if (v.id == R.id.tvEdit) {
                    val intent = Intent(this@WorkVideoActivity, EditVedioActivity::class.java)
                    intent.putExtra("video", videoEntity)
                    startActivity(intent)
                    finish()
                }
                super.onVideoClick(v, tvComment, position, model)
            }
        }
    }

    private fun passData() {
        val map: MutableMap<String, Any> = HashMap()
        //1审核通过 2审核未通过0未审核
        val news: MutableMap<String, String> = HashMap()
        val lists: MutableList<Map<String, String>> =
            ArrayList()
        news["id"] = videoEntity.id
        news["projectCode"] = videoEntity.videoId
        // 1-新闻 2-视频 3-补卡 4-请假 5-外勤 6-图文
        news["type"] = "2"
        news["auditStatus"] = "1"
        news["employeeCode"] = videoEntity.employeeCode
        lists.add(news)
        map["tbKmerWorkFlowProjects"] = XJson.listToJsArrays(lists)
        MyHttpRequest.postRequestJson<String>(
            Urls.auditWorkfolwProject,
            this,
            XJson.mapToJObj(map).toString(),
            object : JsonCallback<String?>() {

                override fun onSuccess(response: Response<String?>?) {
                    try {
                        val jsonObject =
                            JSONObject(response?.body().toString())
                        if ("200".equals(jsonObject.getString("code"))) {
                            MyToast.show("审核成功")
                            EventBus.getDefault()
                                .post(MyEvent("RefreshWork"))
                            finish()
                        } else {
                            if (jsonObject.getString("code").equals("400")) {
                                OkGoUtil.onErrorIntent(
                                    jsonObject.getString("code"),
                                    jsonObject.getString("msg")
                                )
                            } else {
                                MyToast.show(jsonObject.getString("msg"))
                            }
                        }
                    } catch (e: JSONException) {
                        e.printStackTrace()
                        MyToast.show("操作失败，请稍后重试")
                    }
                }
            })
    }

    var hasInitedPlayerData = false
    override fun loadPlayList(isLoadMoreData: Boolean) {
        handler.postDelayed({
            if (hasInitedPlayerData) {
                videoPlayView.completeLoadState(true)
            } else {
                hasInitedPlayerData = true
                videoPlayView.refreshVideoList(arrayListOf(VideoSourceModel(videoEntity)), true)
            }
        }, 1)
    }

    override fun initData() {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    override fun onMoonEvent(messageEvent: MyEvent) {
        //事件订阅者处理事件
        if (messageEvent.hashMap != null) {
            val map: Map<String, String?> =
                messageEvent.hashMap
        } else {
            val msg = messageEvent.message
            if (msg?.equals("DeleteVideo")!!) {
                     finish()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}